<?php

namespace App\Http\Controllers;

use App\Library\ClientIp;
use App\Library\FileSystem;
use App\Library\Utils;
use App\Traits\ResponseTrait;

class AdminController extends Controller
{
    use ResponseTrait;
    protected const QUERY_LIMIT = 200;
    protected const GUARD_NAME = 'admin';
    protected const MENUS_TYPE = [0 => "none", 1 => "menu", 2 => "button", 3 => "iframe", 4 => "link"];

    protected function guard()
    {
        return auth()->guard(self::GUARD_NAME);
    }

    protected function userAllPermissions($request): array
    {
        $user = $request->user();
        return $user->getAllPermissions()->pluck('name')->toArray();
    }

    protected function libUtils(): Utils
    {
        return Utils::init();
    }

    protected function libFileSystem(string $diskName = ''): FileSystem
    {
        if (empty($diskName)) {
            $config = $this->getUploadConfig();
            $diskName = $config['storage'] ?? 'public';
        }
        return FileSystem::init($diskName);
    }

    /**
     * @return array
     */
    protected function getUploadConfig(): array
    {
        return config('custom.upload');
    }

    protected function libClientIp(string $ip = ''): ClientIp
    {
        return ClientIp::init($ip);
    }

    protected function createSyslog(?string $desc, array $properties, $model = null, string $inLog = 'Syslog'): void
    {
        $activity = activity()->inLog($inLog)->causedBy($this->guard()->user());
        $model && $activity->performedOn($model);
        $activity->withProperties($properties)->log($desc);
    }

    protected function sysPerData($row): array
    {
        $arr = [
            'id' => $row['id'],
            'parentId' => $row['parentId'],
            'name' => $row['name'],
            'path' => $row['path'],
            'meta' => [
                'title' => $row['display_name'],
                'icon' => $row['icon'],
                'type' => self::MENUS_TYPE[(int)$row['genre']],
            ],
        ];
        if (!empty($row['component'])) {
            $arr['component'] = $row['component'];
        }
        return $arr;
    }
}
